#include "scheduler.h"
// clang-format off
typedef struct
{
	void (*task_func)(void);
	uint32_t rate_ms;
	uint32_t last_ms;
} task_t;

task_t tasks[] = {
	{btn_task,5,0},
	{battery_task, 800, 0},
	{oled_ui_task, 300, 0},
	{ml307r_get_change_electricity, 800, 0},	//0.8s判断确认标志并更新
	{ml307r_get_electricity,300000, 0},	//5分钟更新一次数据
};

uint8_t task_count;

void scheduler_init(void)
{
	task_count = sizeof(tasks) / sizeof(task_t);
	button_app_init();
	HAL_TIM_Base_Start_IT(&htim3); // 启动定时器3中断
	OLED_Init();
	battery_init();
	OLED_Printf(22, 16, OLED_6X8, "init ml307r...");
    OLED_Printf(22, 32, OLED_6X8, "version:NO RTOS");
	OLED_Update();
	int init_flag = ml307r_init();
	printf("init_flag:%d\r\n", 1);
	OLED_ClearArea(0, 16, 128, 32);
    OLED_Update();
}

void scheduler_run(void)
{
	uint8_t i;
	for (i = 0; i < task_count; i++)
	{
		uint32_t now_time = HAL_GetTick();
		if ((uint32_t)(now_time - tasks[i].last_ms) >= tasks[i].rate_ms)
		{
			tasks[i].last_ms = now_time;
			tasks[i].task_func();
		}
	}
}

